<?php
    if($_GET['view'] == 'write'){
        
        $dirUpload = UP_IMG_PATH.'events/';
        $Upload = new Upload();
        $Upload['WriteMode'] = 1;
        $Upload['DirUpload'] = $dirUpload;
        if (!empty($_POST['submit'])) $Upload->Execute();
        
        $thumb = new Image($dirUpload.$Upload['name'].$Upload['ext']);
        $thumb->createThumb('petit_'.$Upload['name'],90,120);
        $thumb->createThumb('grand_'.$Upload['name'],220,290);

        $event = new Event();
        $event['user_id']       =   $_SESSION['user']['id'];
        $event['categorie_id']  =   $_POST['event_category'];
        $event['titre']         =   $_POST['event_title'];
        $event['vignette']      =   $Upload['name'].$Upload['ext'];
        $event['contenu']       =   $_POST['event_text'];
		$event['date_deb'] 		= $_POST['datebegin'];
		$event['date_fin'] 		= $_POST['dateend'];
		$event['heure']	 		= $_POST['timebegin'];
		$event['lieu'] 			= $_POST['event_ville'];
		$event['cp'] 			= $_POST['event_cp'];
		$event['prix'] 			= $_POST['event_price'];
		$event['pays_id']		= $_POST['event_country'];

        $event->save();
		
        if(isset($_POST['event_keyword'])){
            foreach(explode(',',$_POST['event_keyword']) as $keyword){
                if($keyword != "" && $keyword != " "){
                    $tag = new EventTag();
                    $tag['tag'] = $keyword;
                    $tag['slug'] = EventTag::makeSlug($keyword);
                    $tag['events'] = $event;
                    $tag->save();
                }
            }
        }
        if(isset($_POST['post_products'])){
            foreach(explode(',',$_POST['post_products']) as $product){
                if($product!="" && $product!=" ")$new_post->saveLinkedProduct($product);
            }
        }


        $nf = new NewsFeed('post');
        $nf->addFeed($_SESSION['user'], 'a publié', $new_post);

        
    }
    if($_GET['view']=='change'){
        $old_post=new Post($_GET['idpost']);
        $old_post->load();
        if($_FILES['userfile']['name'][0]!=""){
            
            unlink(UP_IMG_PATH.'articles/'.$old_post['vignette']);
            unlink(UP_IMG_PATH.'articles/petit_'.$old_post['vignette']);
            unlink(UP_IMG_PATH.'articles/grand_'.$old_post['vignette']);
            
            $dirUpload=UP_IMG_PATH.'articles/';
            $Upload = new Upload();
            $Upload['WriteMode']=1;
            $Upload['DirUpload']=$dirUpload;
            if (!Empty($_POST['submit'])) $Upload->Execute();
            
            $thumb = new Image($dirUpload.$Upload['name'].$Upload['ext']);
            $thumb->createThumb('petit_'.$Upload['name'],90,120);
            $thumb->createThumb('grand_'.$Upload['name'],220,290);
            
            $old_post['vignette']       =   $Upload['name'].$Upload['ext'];
        }
            $old_post['categorie_id']   =   $_POST['post_category'];
            $old_post['titre']          =   $_POST['post_title'];
            $old_post['contenu']        =   $_POST['post_text'];
            $old_post['auteur']         =   $_POST['post_from'];
            if(isset($_POST['postblog']))
            $old_post['url']            =   $_POST['postblog'];
            if(isset($_POST['myblog']))
            $old_post['url_blog']       =   $_POST['myblog'];
            $old_post->save();

            if(isset($_POST['post_keyword'])){
                foreach(explode(',',$_POST['post_keyword']) as $keyword){
                    if($keyword!="" && $keyword!=" "){
                        $flag=0;
                        if (sizeof($old_post['tags'])!=0){
                            foreach($old_post['tags'] as $old_tag){
                                $slug=PostTag::makeSlug($keyword);
                                if($slug==$old_tag['slug']) $flag=1;
                            }
                        }
                        if($flag==0){
                            $tag = new PostTag();
                            $tag['tag'] = $keyword;
                            $tag['slug'] = $slug;
                            $tag['articles']=$old_post;
                            $tag->save();
                        }
                    }
                }
            }

            if(isset($_POST['post_products'])){
            foreach(explode(',',$_POST['post_products']) as $product){
                if($product!="" && $product!=" "){
                    $flag=0;
                    if (sizeof($old_post['LinkedProducts'])!=0){
                        foreach($old_post['LinkedProducts'] as $old_linked_products){
                            if($old_linked_products['id']==$product)$flag=1;
                        }
                    }
                    if($flag==0) $old_post->saveLinkedProduct($product);
                }
            }
        }
        
    }
    header('Location:'.URL_PATH.'articles/');
?>